package cn.jdy.plugins.wap;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.socket.TextMessage;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.common.util.WxHttpUtil;
import cn.jdy.common.util.XMLUtil;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Chat;
import cn.jdy.plugins.entity.LuckyBag;
import cn.jdy.plugins.entity.OrderRecord;
import cn.jdy.plugins.entity.RedPacket;
import cn.jdy.plugins.entity.RedPacketReceive;
import cn.jdy.plugins.entity.WatchUser;
import cn.jdy.plugins.service.ChatService;
import cn.jdy.plugins.service.LuckyBagService;
import cn.jdy.plugins.service.RedPacketReceiveService;
import cn.jdy.plugins.service.RedPacketService;
import cn.jdy.plugins.service.WatchUserService;
import cn.jdy.plugins.wap.util.ChatUtil;
import cn.jdy.plugins.wap.util.RedPacketUtil;
import cn.jdy.plugins.websocket.WebsocketEndPoint;

/**
 * @title  操作类
 * @author ningqian
 * @since  2017-4-10
 */

@Controller
@RequestMapping("/redPacket")
public class RedPacketCtrl extends BaseCtrl{
	@Autowired
	private RedPacketService redPacketService;
	@Autowired
	private RedPacketReceiveService rePacketReceiveService;
	@Autowired
	private LuckyBagService luckyBagService;
	@Autowired
	private WatchUserService watchUserService;
	 @Autowired
	 private ChatService chatService;
	
	//微信参数配置  
//	private static String api_key="11111111111111111111111111111111";  
//    private static String appid="wx003d8b600a48f618"; 
//    private static String mch_id="1296239001";  
//    private static String ip = "192.168.1.1";   
    //回调地址  
    private static String notify_url = "http://www.bee-live.cn/w/redPacket/weixinNotify"; //测试  
    
    
    /**
	 * 登陆初始化
	 */
	@SuppressWarnings("static-access")
	@RequestMapping("toPay/")
	public ModelAndView forLogin(String url) {
		if(null==url){
			url="1";
		}
		ModelAndView mv = new ModelAndView(super.t + "/testImg.jsp");
		mv.addObject("url", url);
		return mv;
	}
	
    /**
     * 微信统一下单接口
     * @param rpType   红包类别 1-随机 2-固定
     * @param rpCount  红包数量
     * @param single   单个金额，固定红包时有值
     * @param total    总额
     * @param wishMsg  红包祝福语
     * @param openId   红包发送者唯一id
     * @param nickName 红包发送者昵称
     * @return
     */
    @RequestMapping("wxprepaydo") 
    @ResponseBody
    public Map wxprepaydo(@RequestParam("rpType") String rpType,@RequestParam("rpCount") int rpCount,
    		@RequestParam("single") double single,@RequestParam("total") double total,@RequestParam("wishMsg") String wishMsg,
    		@RequestParam("openId") String openId,@RequestParam("nickName") String nickName,@RequestParam("liveId") Long liveId,
    		@RequestParam("appid") String appid,@RequestParam("mchid") String mchid,@RequestParam("apikey") String apikey) { 
    	
		BaseLog.e(this.getClass(), "发红包->openId:"+openId+",nickName:"+nickName+",liveId:"+liveId
				+",total:"+total+",appid:"+appid+",mchid="+mchid+",apikey:"+apikey);
    	SortedMap<Object, Object> payMap = new TreeMap<Object, Object>();
    	String result="ok";
	    try {
	          String time_start = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	      	  Calendar ca = Calendar.getInstance();
	    	  ca.setTime(new Date());
	    	  ca.add(Calendar.DATE, 1);
	    	  String time_expire = new SimpleDateFormat("yyyyMMddHHmmss").format(ca.getTime());
	          
	          String out_trade_no = readyOrder(openId, nickName,liveId, rpType, rpCount, single, total, wishMsg, mchid); 
	          
	      	  SortedMap<Object, Object> paraMap = new TreeMap<Object, Object>();
	          paraMap.put("appid", appid); 
	          paraMap.put("mch_id", mchid); 
	          paraMap.put("nonce_str", RedPacketUtil.pay_nonce_str()); 
	          
	          if(rpType.equals("3")){
	        	    paraMap.put("body", "支付"); 
	  	          paraMap.put("attach", "付费节目"); 
	          }else if(rpType.equals("4")){
	        	    paraMap.put("body", "打赏"); 
	  	          paraMap.put("attach", "打赏作者"); 
	          }else{
	        	    paraMap.put("body", "红包支付"); 
	  	          paraMap.put("attach", "发红包"); 
	          }
	      
	          paraMap.put("out_trade_no", out_trade_no);  
	          paraMap.put("total_fee", RedPacketUtil.get_total_fee(total));   //钱为分 
	          paraMap.put("spbill_create_ip", RedPacketUtil.getHostAddress());
	          paraMap.put("time_start", time_start);
	          paraMap.put("time_expire", time_expire);
	          
	          String nurl=notify_url+"/"+out_trade_no+".html";
	          paraMap.put("notify_url", nurl); 
	          paraMap.put("trade_type", "JSAPI"); 
	          paraMap.put("openid", openId); 
	          String sign = RedPacketUtil.createSign("UTF-8", paraMap, apikey);
	          paraMap.put("sign", sign); 
	          BaseLog.e(this.getClass(), "加加--："+paraMap.toString());
	          BaseLog.e(this.getClass(), "加加22："+nurl);
	          //统一下单
	          String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	          String requestXML = RedPacketUtil.getRequestXml(paraMap);
	  		  String xmlStr = WxHttpUtil.postData(url, requestXML);
	  		  
	  		  BaseLog.e(this.getClass(), xmlStr.toString());
	          
	          // 预付商品id 
	          String prepay_id = ""; 
	         
	          if(xmlStr.indexOf("SUCCESS") != -1) {   
	        	  Map<String, String> map = XMLUtil.doXMLParse(xmlStr); 
	        	  String return_msg = map.get("return_msg");
	        	  if(StringUtil.isNotEmpty(return_msg) && return_msg.equals("OK")){
	        		  prepay_id = (String) map.get("prepay_id"); 
	        	  }else{
	        		  result = "error";
	        	  }
	          }else{
	        	  result = "error";
	          }
	        
	          if("ok".equals(result)){
	        	  payMap.put("appId", appid); 
	        	  payMap.put("timeStamp", ChatUtil.create_timestamp()); 
	        	  payMap.put("nonceStr", RedPacketUtil.pay_nonce_str()); 
	        	  payMap.put("package", "prepay_id=" + prepay_id); 
	        	  payMap.put("signType", "MD5"); 
	        	 
	        	  String paySign = RedPacketUtil.createSign("UTF-8", payMap, apikey); 
	        	  payMap.put("paySign", paySign); 
	          }
	          payMap.put("prepay_id", prepay_id); 
	          payMap.put("result", result);
	          payMap.put("orderNo", out_trade_no);
		} catch (Exception e) {
			result="error";
			BaseLog.e(this.getClass(), "下单失败！", e);
		}
	    return payMap;
    } 
    
    
    /**
     * 微信统一下单接口
     * @param rpType   类别 1-打赏 2-支付直播费用
     * @param rpCount  红包数量
     * @param single   单个金额，固定红包时有值
     * @param total    总额
     * @param wishMsg  红包祝福语
     * @param openId   红包发送者唯一id
     * @param nickName 红包发送者昵称
     * @return
     */
    @RequestMapping("wxprepaydsdo") 
    @ResponseBody
    public Map wxprepaydsdo(@RequestParam("rpType") String rpType,@RequestParam("rpCount") int rpCount,
    		@RequestParam("single") double single,@RequestParam("total") double total,@RequestParam("wishMsg") String wishMsg,
    		@RequestParam("openId") String openId,@RequestParam("nickName") String nickName,@RequestParam("liveId") Long liveId,
    		@RequestParam("appid") String appid,@RequestParam("mchid") String mchid,@RequestParam("apikey") String apikey) { 
    	
		BaseLog.e(this.getClass(), "发红包->openId:"+openId+",nickName:"+nickName+",liveId:"+liveId
				+",total:"+total+",appid:"+appid+",mchid="+mchid+",apikey:"+apikey);
    	SortedMap<Object, Object> payMap = new TreeMap<Object, Object>();
    	String result="ok";
	    try {
	          String time_start = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	      	  Calendar ca = Calendar.getInstance();
	    	  ca.setTime(new Date());
	    	  ca.add(Calendar.DATE, 1);
	    	  String time_expire = new SimpleDateFormat("yyyyMMddHHmmss").format(ca.getTime());
	    	     if(rpType.equals("1")){
	    	    	 rpType="3";//
		          } else  if(rpType.equals("2")){
		        	  rpType="4";
		          }
		    	          String out_trade_no = readyOrder(openId, nickName,liveId, rpType, rpCount, single, total, wishMsg, mchid); 
	          
	      	  SortedMap<Object, Object> paraMap = new TreeMap<Object, Object>();
	          paraMap.put("appid", appid); 
	          paraMap.put("mch_id", mchid); 
	          paraMap.put("nonce_str", RedPacketUtil.pay_nonce_str()); 
	          if(rpType.equals("1")){
	              paraMap.put("body", "支付"); 
		          paraMap.put("attach", "打赏"); 
	          } else  if(rpType.equals("2")){
	              paraMap.put("body", "支付"); 
		          paraMap.put("attach", "支付费用"); 
	          }
	    
	          paraMap.put("out_trade_no", out_trade_no);  
	          paraMap.put("total_fee", RedPacketUtil.get_total_fee(total));   //钱为分 
	          paraMap.put("spbill_create_ip", RedPacketUtil.getHostAddress());
	          paraMap.put("time_start", time_start);
	          paraMap.put("time_expire", time_expire);
	          paraMap.put("notify_url", notify_url); 
	          paraMap.put("trade_type", "JSAPI"); 
	          paraMap.put("openid", openId); 
	          String sign = RedPacketUtil.createSign("UTF-8", paraMap, apikey);
	          paraMap.put("sign", sign); 
	        
	          //统一下单
	          String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	          String requestXML = RedPacketUtil.getRequestXml(paraMap);
	  		  String xmlStr = WxHttpUtil.postData(url, requestXML);
	  		  
	  		  BaseLog.e(this.getClass(), xmlStr.toString());
	          
	          // 预付商品id 
	          String prepay_id = ""; 
	         
	          if(xmlStr.indexOf("SUCCESS") != -1) {   
	        	  Map<String, String> map = XMLUtil.doXMLParse(xmlStr); 
	        	  String return_msg = map.get("return_msg");
	        	  if(StringUtil.isNotEmpty(return_msg) && return_msg.equals("OK")){
	        		  prepay_id = (String) map.get("prepay_id"); 
	        	  }else{
	        		  result = "error";
	        	  }
	          }else{
	        	  result = "error";
	          }
	        
	          if("ok".equals(result)){
	        	  payMap.put("appId", appid); 
	        	  payMap.put("timeStamp", ChatUtil.create_timestamp()); 
	        	  payMap.put("nonceStr", RedPacketUtil.pay_nonce_str()); 
	        	  payMap.put("package", "prepay_id=" + prepay_id); 
	        	  payMap.put("signType", "MD5"); 
	        	 
	        	  String paySign = RedPacketUtil.createSign("UTF-8", payMap, apikey); 
	        	  payMap.put("paySign", paySign); 
	          }
	          payMap.put("prepay_id", prepay_id); 
	          payMap.put("result", result);
	          payMap.put("orderNo", out_trade_no);
		} catch (Exception e) {
			result="error";
			BaseLog.e(this.getClass(), "下单失败！", e);
		}
	    return payMap;
    } 
    

	/**
	 * 抢红包
	 */
	@RequestMapping("robRedPacket") 
	@ResponseBody
	public synchronized Map<String,String> robRedPacket(@RequestParam("orderNo") String orderNo,@RequestParam("userOpenId") String userOpenId,
			@RequestParam("userNickName") String userNickName,@RequestParam("userHeadImg") String userHeadImg){
		Map<String,String> map = new HashMap<String,String>();
		String sign="0"; //0-无红包信息或异常，1-红包已抢完，2-当前还存在红包，3-未领取，4-已领取，
		String msg = "";   
		String rpSendId = "0";
		try {
			RedPacket red = redPacketService.findOne("orderNo",orderNo,"sendStatus","1");
			if(null != red){
				rpSendId = String.valueOf(red.getId());
				//判断是否领取过该红包
				RedPacketReceive rr = rePacketReceiveService.findOne("rpSendId",red.getId(),"watchUserId",userOpenId);
				if(null ==rr){
					sign = "3";  //未领取过红包
					//红包数量大于0且状态为未领取完
					if(0< red.getRpCount() && "0".equals(red.getGetStatus())){
						sign = "2";
						if("1".equals(red.getRpType())){ //1.随机红包
							List<LuckyBag> list = luckyBagService.find("rpId",red.getId()); //获取随机列表
							if(0< list.size() && null != list){
								LuckyBag bag = list.get(0);
								//可以领取的随机红包金额,剩余总额
								double getMoney = bag.getLuckMoney(); 
								
								//1.1领取红包
								RedPacketReceive receive = new RedPacketReceive();
								receive.setLiveId(red.getLiveId());
								receive.setRpSendId(red.getId());
								receive.setWatchUserId(userOpenId);
								receive.setWatchUsername(userNickName);
								receive.setWatchUserHeadImg(userHeadImg);
								receive.setReceiveMoney(String.valueOf(getMoney));
								receive.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
								rePacketReceiveService.save(receive);
								
								msg = "恭喜抢到"+getMoney+"元";
								
								//1.2修改用户总额
								WatchUser user = watchUserService.findOne("userOpenId",userOpenId);
								if(null != user){
									//当前余额 = 总额+红包领取金额
									double currentMoney  = RedPacketUtil.add(user.getTotal(), getMoney);
									user.setTotal(currentMoney);
									user.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
									watchUserService.update(user);   //修改用户账户总额
								}
								
								
								//1.3修改红包明细
								int currentCount = red.getRpCount()-1;
								red.setRpCount(currentCount);
//								red.setTotal(red.getTotal()-getMoney);
								if(currentCount == 0){
									red.setGetStatus("1");
								}
								red.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
								redPacketService.update(red);
								
								//1.3删除已经领取的随机红包
								luckyBagService.delete(bag.getId());
								
							}else{
								sign = "1";
								msg = "红包已被抢完";
							}
							
							
						}else { //2.领取固定红包
							RedPacketReceive receive = new RedPacketReceive();
							//2.1 可以领取的红包金额
							double getMoney = red.getSingle(); 
							receive.setLiveId(red.getLiveId());
							receive.setRpSendId(red.getId());
							receive.setWatchUserId(userOpenId);
							receive.setWatchUsername(userNickName);
							receive.setWatchUserHeadImg(userHeadImg);
							receive.setReceiveMoney(String.valueOf(getMoney));
							receive.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
							rePacketReceiveService.save(receive);
							msg = "恭喜抢到"+getMoney+"元";
							
							//2.2修改用户总额
							WatchUser user = watchUserService.findOne("userOpenId",userOpenId);
							if(null != user){
								//当前余额 = 总额+红包领取金额
								double currentMoney  = RedPacketUtil.add(user.getTotal(), getMoney);
								user.setTotal(currentMoney);
								user.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
								watchUserService.update(user);   //修改用户账户总额
							}
							
							//2.3领取红包后,修改红包信息
							int currentCount = red.getRpCount()-1;
							red.setRpCount(currentCount);
//							red.setTotal(red.getTotal()-getMoney);
							if(currentCount == 0){
								red.setGetStatus("1");
							}
							
							red.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
							redPacketService.update(red);
						}
					}else{
						//红包已被抢完
						sign = "1";
						msg = "红包已被抢完";
					}
				}else{
					sign = "4";
					msg="已经领取过";
				}
			}else{
				sign = "0";
				msg = "无红包信息"; 
			}
		} catch (Exception e) {
			sign = "0";   
			msg = "异常";
			BaseLog.e(this.getClass(), "抢红包异常", e);
		}
		
		map.put("sign", sign);
		map.put("msg", msg);
		map.put("rpSendId", rpSendId);  //红包id
		return map;
	}
	
	
	/**
	 * 查看他人手气
	 * @param rpSendId
	 * @return
	 */
	@RequestMapping("/othersLucky")
	public ModelAndView othersLucky(RedPacketReceive receive) {
		ModelAndView mv = new ModelAndView("/red/othersLucky.jsp");
		RedPacket red = redPacketService.find(receive.getRpSendId());
		List<RedPacketReceive> list = new ArrayList<RedPacketReceive>();
		list = rePacketReceiveService.find("rpSendId", receive.getRpSendId());
		mv.addObject("datalist", list);
		mv.addObject("robCount",rePacketReceiveService.count("rpSendId", receive.getRpSendId()));
		mv.addObject("redPacket",red);
		return mv;
	}
	

	
    /**
     * 生成红包订单
     * @return
     */
    public String readyOrder(String openid,String nickName,Long liveId,String rpType,
    		int rpCount,double single,double total,String wishMsg,String mchid){
    	//订单号
    //	String orderNo = ChatUtil.create_timestamp();
    	String orderNo = ChatUtil.generateString(18);
    	
    	RedPacket redP = new RedPacket();
    	redP.setOrderNo(orderNo);
    	redP.setMch_id(mchid);
    	redP.setLiveId(liveId);
    	redP.setUserOpenId(openid);
    	redP.setUserNickName(nickName);
    	redP.setRpType(rpType);
    	redP.setRpCount(rpCount);
    	redP.setSingle(single);
    	redP.setTotal(total);
    	redP.setWishMsg(wishMsg);
    	redP.setSendStatus("0");  //未支付
    	redP.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
    	redPacketService.save(redP);
    	return redP.getOrderNo();
    }
  
    
    
	/**
	 * 支付成功后修改红包状态
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("updateRedPacket") 
	@ResponseBody
    public Map<String,String> updateRedPacket(@RequestParam("orderNo") String orderNo) {
		Map<String,String> map = new HashMap<String,String>();
		String result ="ok";
		try {
			RedPacket redData = redPacketService.findOne("orderNo",orderNo);
			redData.setSendStatus("1");  //支付成功
			redData.setGetStatus("0");   //待领取
			redData.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			redPacketService.update(redData);
			
			//如果是随机红包，分配随机红包
			getRamdomHb(redData.getTotal(), redData.getRpCount(), 0.01, redData.getId());
			
		} catch (Exception e) {
			result = "error";
		}
		map.put("result", result);
		return map; 
	}
 
	
	  
	/**
	 * 支付失败后删除红包信息
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("deleteRedPacket") 
	@ResponseBody
    public Map<String,String> deleteRedPacket(@RequestParam("orderNo") String orderNo) {
		Map<String,String> map = new HashMap<String,String>();
		String result ="ok";
		try {
			RedPacket redData = redPacketService.findOne("orderNo",orderNo);
			if(null != redData && "0".equals(redData.getSendStatus())){
				redPacketService.delete(redData.getId());
			}
		} catch (Exception e) {
			result = "error";
		}
		map.put("result", result);
		return map; 
	}
	
	
	//分配随机红包
	public void getRamdomHb(double total, int num, double min,long rpId){
		try {
			for(int i = 1; i < num; i++){
				LuckyBag bag = new LuckyBag();
				double safe_total = (total-(num-i)*min)/(num-i);
				double money = Math.random()*(safe_total-min)+min;
				BigDecimal money_bd = new BigDecimal(money);
				money = money_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
				total = total-money;
				BigDecimal total_bd = new BigDecimal(total);
				total = total_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
				System.out.println("第"+i+"个红包金额："+money+",余额："+total);
				bag.setRpId(rpId);
				bag.setRedSort(i);
				bag.setLuckMoney(money);
				bag.setTotal(total);
				bag.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				luckyBagService.save(bag);
			}
			
			System.out.println("第"+num+"个红包金额："+total+",余额：0.00");
			LuckyBag lastBag = new LuckyBag();
			lastBag.setRpId(rpId);
			lastBag.setRedSort(num);
			lastBag.setLuckMoney(total);
			lastBag.setTotal(0.00);
			lastBag.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			luckyBagService.save(lastBag);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "分配随机红包异常", e);
		}
	}

	
   /**
	 * 微信回调
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/weixinNotify/{id}.html")
	public ModelAndView weixinNotify(@PathVariable String id) throws Exception {
		ModelAndView mv = new ModelAndView();
	
	 
		RedPacket redData = redPacketService.findOne("orderNo",id);
		if(redData.getSendStatus().equals("0")){  //判断是否已经支付，不然会重复回调
			
		redData.setSendStatus("1");  //支付成功
		redData.setGetStatus("0");   //待领取
		redData.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
		redPacketService.update(redData);
		//如果是随机红包，分配随机红包
		getRamdomHb(redData.getTotal(), redData.getRpCount(), 0.01, redData.getId());
		 String msg="";//信息
		 String chatType="";//类别
		 if(redData.getRpType().equals("4")){
			 chatType = "4";//打赏信息
			 msg = redData.getTotal() + ";" + redData.getWishMsg();
			 BaseLog.e(this.getClass(), "处理客户端发来的消息......+"+msg);
			 System.out.println(msg);
		 }else{
			 chatType = "3";
			 msg = id + ";" + redData.getWishMsg();
		 }
		 WatchUser user= watchUserService.find("userOpenId",redData.getUserOpenId()).get(0);
		 
		 	Chat chat = new Chat();
	    	chat.setLiveId(redData.getLiveId());
	    	chat.setUserOpenId(user.getUserOpenId());
	    	chat.setUserNickName(user.getUserNickName());
	    	chat.setUserHeadImg(user.getUserHeadImg());
	    	chat.setUserSex(user.getUserSex());
	    	chat.setChatType(chatType);
	    	chat.setChatContent(msg);
	    	chat.setStatus("0");
	    	String createTime = DateUtil.getCurrentDate(DateUtil._DEFAULT2);
	    	chat.setCreateTime(createTime);
	    	chatService.save(chat);
	    	
	    	//屏蔽脏字
	    	String reg = "[艹|日|靠|槽|逼|草|婊|淫|骚]";
			String reegmsg = msg.replaceAll(reg, "*");
	    String json = "{'liveId':"+redData.getLiveId()+",'userOpenId':'"+user.getUserOpenId()+"','userNickName':'"+user.getUserNickName()+"','userHeadImg':'"+user.getUserHeadImg()+"',"
	    			+ "'chatType':'"+chatType+"','message':'"+reegmsg+"','createTime':'"+redData.getCreateTime()+"'}";
      	BaseLog.e(this.getClass(), "可以发送吗"+json);

	    
	    WebsocketEndPoint wp=new WebsocketEndPoint();
	  //  wp.saveChat(redData.getLiveId()+"",user.getUserOpenId(),user.getUserNickName(),user.getUserHeadImg(),user.getUserSex(),chatType,msg);
		TextMessage textMessage = new TextMessage(json);
		wp.sendMessageToUsers(textMessage);
		}
		return mv;
	}
	
	 /**
		 * 修改订单状态
		 * @param orderNo
		 * @return
		 * @throws Exception
		 */
		@RequestMapping("/saveorderNoLog")
		@ResponseBody
		public Map<String,String>  saveorderNoLog(String orderNo) throws Exception {
			Map<String,String> map = new HashMap<String,String>();
			String result ="ok";
			try {
 
		redPacketService.updateOrder(orderNo);
			} catch (Exception e) {
				result = "error";
			}
			map.put("result", result);
			return map; 
		}
	    

	
  
	  
}
